<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app"></div>

  <template id="my-app">
    <h2>当前温度：{{temp}}</h2>
    <h3>建议穿：{{cloth}}</h3>

    <button @click="minus">-5</button>
    <button @click="add">+5</button>

  </template>


  <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.3.4/vue.global.js"></script>
  <script>
    Vue.createApp({
      template: '#my-app',
      data() {
        return {
          temp: 30,
          cloth: '夹克'
        }
      },
      watch: {
        temp: {
          handler: function (newVal, oldVal) {
            if (newVal <= 10) {
              this.cloth = '棉袄'
            } else if (newVal <= 20) {
              this.cloth = '夹克'
            } else {
              this.cloth = '短袖'
            }
          },
          immediate: true  // 立即执行
        }
        // temp(newVal, oldVal) {
        //   // console.log(newVal, oldVal);
        //   if (newVal <= 10) {
        //     this.cloth = '棉袄'
        //   } else if (newVal <= 20) {
        //     this.cloth = '夹克'
        //   } else {
        //     this.cloth = '短袖'
        //   }
        // }
      },
      methods: {
        minus() {
          this.temp -= 5
        },
        add() {
          this.temp += 5
        }
      }
    }).mount('#app')
  </script>
</body>

</html>